<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profile.photo"
            @click="clickImg"
          />
          <!-- file 选择框 -->
          <!-- 视觉上隐藏, 标签还在DOM树上, 还可以触发功能 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="changeIpt"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profile.name" @click="showName" />
      <van-cell
        title="生日"
        is-link
        :value="profile.birthday"
        @click="showBir"
      />
    </van-cell-group>
    <!-- 修改昵称 -->
    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      @confirm="confirmEv"
      :before-close="beforeClose"
    >
      <input type="text" v-focus v-model="iptTxt" />
    </van-dialog>

    <!-- 修改日期 -->
    <van-popup
      round
      v-model="showbir"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="cancelPicker"
        @confirm="confirmPicker"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserInfo, updateAvatar, updateUserNickname } from "@/api/index";
import { Notify } from "vant";
import {formatDate} from '@/utils/date'
export default {
  data() {
    return {
      profile: {},
      show: false,
      showbir: false,
      iptTxt: "",
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2021, 0, 17),
    };
  },
  async created() {
    let res = await getUserInfo();
    this.profile = res.data.data;
  },
  methods: {
    async changeIpt(e) {
      if (e.target.files.length === 0) return;
      const fd = new FormData();
      fd.append("photo", e.target.files[0]);
      let res = await updateAvatar(fd);
      this.profile.photo = res.data.data.photo;
    },
    clickImg() {
      this.$refs.iptFile.click();
    },
    showName() {
      this.show = true;
      this.iptTxt = this.profile.name;
    },
    async beforeClose(action, done) {
      if (action === "confirm") {
        let reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/;
        if (reg.test(this.iptTxt) === true) {
          await updateUserNickname(this.iptTxt);
          this.profile.name = this.iptTxt;
          done();
        } else {
          Notify({ type: "warning", message: "请输入1-7位中英文结合的名称" });
          done(false);
        }
      } else {
        done();
      }
    },
    confirmEv() {},
    showBir() {
      this.showbir = true;
      this.currentDate=new Date(this.profile.birthday)
    },
    cancelPicker(){
        this.showbir = false
    },
    async confirmPicker(){
        await updateUserNickname({birthday:formatDate(this.currentDate)})
        this.showbir=false
        this.profile.birthday=formatDate(this.currentDate)
    }
  },
};
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}

::v-deep .van-dialog__content {
  text-align: center;
  input {
    padding: 2px;
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>